package com.goparry.taskman.repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Task implements Comparable<Task>{
	static SimpleDateFormat FORMATTER_SET = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat FORMATTER_GET = new SimpleDateFormat("yyyy-MM-dd");
	private String name;
	private String title;
	private String type;
	private String instructions;
	private String step;
	private Date receivedDate;
	private Date dueDate;
	private String documents;
	private int[] documentIDs;
	private int numberOfDocs;
	public static SortBy sortBy;
	public enum SortBy { NAME, DATE, TYPE }
		
	// Task Constructor to explicitly create a new Task
	// Used for development
	public Task(String name, String title, String type, String instructions,
			String step, String receivedDate, String dueDate) {
		super();
		this.name = name;
		this.title = title;
		this.type = type;
		this.instructions = instructions;
		this.step = step;
		setReceivedDate(receivedDate);
		setDueDate(dueDate);
	}
	
	public Task() {
		super();
	}
	
	public Task copy() {
		return new Task(getName(), getTitle(), getType(), getInstructions(), getStep(), getReceivedDate(), getDueDate());
	}
	@Override
	public String toString() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getInstructions() {
		return instructions;
	}

	public void setInstructions(String instructions) {
		this.instructions = instructions;
	}

	public String getStep() {
		return step;
	}

	public void setStep(String step) {
		this.step = step;
	}

	public String getReceivedDate() {
		return FORMATTER_GET.format(receivedDate);
	}
	public Date getReceivedDateAsDate() {
		return receivedDate;
	}
	public void setReceivedDate(String receivedDate) {
		try {
			this.receivedDate = FORMATTER_SET.parse(receivedDate.trim());
		} catch (ParseException e) {
			this.receivedDate = null;
		}
	}

	public String getDueDate() {
		return FORMATTER_GET.format(dueDate);
	}
	public Date getDueDateAsDate() {
		return dueDate;
	}

	public void setDueDate(String dueDate) {
		try {
			this.dueDate = FORMATTER_SET.parse(dueDate.trim());
		} catch (ParseException e) {
			this.dueDate = null;
		}
	}

	public String getDocuments() {
		return documents;
	}

	public void setDocuments(String documents) {
		this.documents = documents;
	}
	
	public int[] getDocumentIDs() {
		return documentIDs;
	}

	public String getDocumentIDs(String delimiter) {
		String sIDs = Integer.toString(documentIDs[0]);
		for (int i=0 ; i<numberOfDocs ; i++) {
			sIDs += delimiter + Integer.toString(documentIDs[i]);
		}
		return sIDs;
	}

	public void setDocumentIDs(int[] documentIDs) {
		this.documentIDs = documentIDs;
	}

	public void setDocumentIDs(String sDocuments) {
		String sIDs[] = sDocuments.split("/");
		
		int documentIDs[] = new int[sIDs.length];
		for (int i=0 ; i<sIDs.length ; i++) {
			documentIDs[i] = Integer.parseInt(sIDs[i]);
		}
		this.documentIDs = documentIDs;
		this.numberOfDocs = documentIDs.length;
	}

	public int getNumberOfDocs() {
		return numberOfDocs;
	}

	public void setNumberOfDocs(int numberOfDocs) {
		this.numberOfDocs = numberOfDocs;
	}
	public SortBy getSortBy() {
		return sortBy;
	}
	public void setSortBy(SortBy sortBy) {
		Task.sortBy = sortBy;
	}
	public int compareTo(Task another) {
		switch (sortBy) {
		case NAME:
			return name.compareToIgnoreCase(another.getName());
		case DATE:
			return dueDate.compareTo(another.getDueDateAsDate());
		case TYPE:
			return type.compareToIgnoreCase(another.getType());
		default:
			return 0;
		}
	}


}
